package com.vancouver.knows.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.pangu.bean.PageInfo;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.MyFollowedInfo;
import com.vancouver.knows.business.service.impl.MyArticleHandle;
import com.vancouver.knows.business.task.mark.FollowedTaskMark;
import com.vancouver.knows.business.task.mark.UserDeleteUpvoteTaskMark;
import com.vancouver.knows.business.task.mark.UserUpvoteTaskMark;

public class FollowedListView extends PullListItemBrowser<MainApplication> {

	private BusinessModule bModule;
	private int type;
	private final int MY_FOLLOWED_ALL = 0;
	private final int MY_FOLLOWED_POST = 1;
	private final int MY_FOLLOWED_EVENT = 2;
	private final int MY_FOLLOWED_BUSINESS = 3;
	private String[] titles;

	@SuppressWarnings("rawtypes")
	public FollowedListView(Context context, ARefreshViewController mController) {
		super(context, mController);
		init();
	}

	public FollowedListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		bModule = imContext.getBusinessModule();
		titles = getResources().getStringArray(R.array.my_followed_array);
	}

	private String returnPostType() {
		String postType = null;
		if (type == MY_FOLLOWED_ALL) {
			postType = null;
		} else if (type == MY_FOLLOWED_POST) {
			postType = "post";
		} else if (type == MY_FOLLOWED_EVENT) {
			postType = "event";
		} else if (type == MY_FOLLOWED_BUSINESS) {
			postType = "business";
		}

		return postType;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		MyFollowedInfo followedInfo = (MyFollowedInfo) ((FollowedAdapter) getAdapterView().getAdapter())
				.getItem(position);
		Class cla = null;
		if (followedInfo.getPostType().equals("post") || followedInfo.getPostType().equals("userpost")) {
			cla = CityNewsDetailActivity.class;
		} else if (followedInfo.getPostType().equals("event")) {
			cla = EventDetailActivity.class;
		} else if (followedInfo.getPostType().equals("business")) {
			cla = CityDiscountDetailActivity.class;
		}
		if (cla != null) {
			Intent intent = new Intent(getContext(), cla);
			intent.putExtra("postId", followedInfo.getId());
			getContext().startActivity(intent);
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new FollowedAdapter();
	}

	public void initLoadableView(int type, ATaskMark taskWraper) {
		this.type = type;
		super.initLoadableView(taskWraper);
	}

	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
		FollowedTaskMark followedTaskMark = (FollowedTaskMark) taskMark;
		PageInfo pageInfo = followedTaskMark.getPageInfo();
		BSharedPrefManager sharedPrefManager = imContext.getSharedPrefManager();
		bModule.getServiceWrapper().myArticleFollowAndComments(this, followedTaskMark, sharedPrefManager.getUserId(),
				String.valueOf(pageInfo.getNextFrom()), String.valueOf(pageInfo.getSize()), returnPostType(),
				MyArticleHandle.USER_FOLLOWED_ITEMS);

	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		super.receiveResult(taskMark, exception, trackerResult);

		if ((taskMark instanceof UserUpvoteTaskMark || taskMark instanceof UserDeleteUpvoteTaskMark)
				&& taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER) {
			notifyDataSetChanged();
		}
	}

	public class FollowedAdapter extends BaseAdapter {

		private LayoutInflater inflater;

		public FollowedAdapter() {
			inflater = LayoutInflater.from(getContext());
		}

		@Override
		public int getCount() {
			return bModule.getCacheManager().getMyFollowedCache().getItemInfoCount(mTaskMark);
		}

		@Override
		public Object getItem(int position) {
			return bModule.getCacheManager().getMyFollowedCache().getItemInfoByTypeIndex(mTaskMark, position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (position == (getCount() - 1)) {
				tryQueryNewItems(true, ATaskMark.TYPE_DEFAULT);
			}

			if (convertView == null) {
				convertView = createItemView(parent);
			}

			MyFollowedInfo myFollowedInfo = (MyFollowedInfo) getItem(position);
			if (myFollowedInfo != null) {
				initItemView(convertView, myFollowedInfo);
			}

			return convertView;
		}

		private View createItemView(ViewGroup parent) {
			View view = inflater.inflate(R.layout.my_followed_view, parent, false);
			ViewHolder viewHolder = new ViewHolder(view);
			view.setTag(viewHolder);

			return view;
		}

		private void initItemView(View convertView, MyFollowedInfo followedInfo) {
			ViewHolder viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.title.setText(followedInfo.getPostTitle());
			viewHolder.titleType.setText(imContext.getString(R.string.owned_plates) +  getPostTextByType(followedInfo.getPostType()));
			viewHolder.followTime.setText(imContext.getString(R.string.followed_time) + followedInfo.getDateAddedGmt());

		}

		private String getPostTextByType(String type){
			if (titles != null && titles.length > 0){
				if ("post".equals(type) || "userpost".equals(type)) {
					return titles[1];
				} else if ("event".equals(type)) {
					return titles[2];
				} else if ("business".equals(type)) {
					return titles[3];
				}
			}

			return "";
		}

		public class ViewHolder {

			@Bind(R.id.title_text)
			TextView title;
			@Bind(R.id.title_type)
			TextView titleType;
			@Bind(R.id.followed_time)
			TextView followTime;

			public ViewHolder(View view) {
				ButterKnife.bind(this, view);
			}

		}

	}

}
